package com.ruoyi.psi.base.product.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.base.cost.domain.ProductionCostListDto;
import com.ruoyi.psi.base.cost.service.IProductionCostService;
import com.ruoyi.psi.base.product.domain.*;
import com.ruoyi.psi.base.product.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品信息
 */
@RestController
@RequestMapping("/psi/base/produce")
@Api(value = "产品信息模块", tags = "产品信息接口")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductionCostService productionCostService;

    /**
     * 获取产品列表
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取产品列表",notes = "查询所有产品信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProductCond cond) {
        startPage();
        List<ProductDto> productDtos = productService.selectProductList(cond);
        return getDataTable(productDtos);
    }

    /**
     * 根据编号获取详细信息
     */
    @GetMapping(value = "/{produceId}")
    @ApiOperation(value = "根据编号获取详细信息",notes = "根据编号获取详细信息")
    public AjaxResult getInfo(@PathVariable String produceId)
    {
        return AjaxResult.success(productService.selectProductById(produceId));
    }

    /**
     * 导出产品
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody ProductCond cond) {
        List<ProductDto> list = productService.selectProductList(cond);
        ExcelUtil<ProductDto> util = new ExcelUtil<>(ProductDto.class);
        return util.exportExcel(list, "产品数据");
    }

    /**
     * 新增产品
     * @param product
     * @return
     */
    @ApiOperation(value = "新增产品", notes = "新增产品")
    @ApiImplicitParam(name = "product", value = "新增产品", dataType = "ProductDomain")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ProductDomain product) {

        try {
            return toAjax(productService.insertProduct(product));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败！");
        }
    }

    /**
     * 修改产品
     * @param product
     * @return
     */
    @ApiOperation(value = "修改产品", notes = "修改产品")
    @ApiImplicitParam(name = "product", value = "修改产品", dataType = "ProductDomain")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody ProductDomain product) {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除产品
     * 仅修改删除标志位
     * @param product
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ProductDomain product) {

        return toAjax(productService.updateProductDeleteFlag(product));
    }

    /**
     * 查询所有产品的利润
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询所有产品的利润",notes = "查询所有产品的利润")
    @PostMapping("/profit")
    public TableDataInfo selectProductionProfit(@RequestBody ProductCond cond) {
        startPage();
        List<ProductionProfit> productionProfitList = productService.selectProductProfit(cond);
        return getDataTable(productionProfitList);
    }

    /**
     * 查询具体某产品成本来源
     * @param produceId
     * @return
     */
    @ApiOperation(value = "查询具体某产品成本来源",notes = "查询具体某产品成本来源")
    @GetMapping("/cost/{produceId}")
    public TableDataInfo selectProductionCostById(@PathVariable String produceId) {
        startPage();
        List<ProductionCostListDto> productionCostListDtoList = productionCostService.selectProductionCostListListById(produceId);
        return getDataTable(productionCostListDtoList);
    }

    /**
     * 查询各自不同种类产品的库存
     * 分月
     * @return
     */
    @ApiOperation(value = "查询各自不同种类产品的库存（分月）",notes = "查询各自不同种类产品的库存（分月）")
    @GetMapping("/selectProductInventoryByCategoryId")
    public AjaxResult selectProductInventoryByCategoryId(@RequestParam String baseCategoryName) {
        List<ProductInventory> productInventories = productService.selectProductInventoryByCategoryId(baseCategoryName);
        return AjaxResult.success("查询成功", productInventories);
    }
}
